package com.js.downloadmanagerdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.js.downloadmanagerdemo.databinding.ActivityMainBinding;
import com.js.downloadmanagerdemo.downloader.DownLoaderManager;
import com.js.downloadmanagerdemo.downloader.DownLoaderUtils;

import constant.UiType;
import listener.Md5CheckResultListener;
import listener.UpdateDownloadListener;
import model.UiConfig;
import model.UpdateConfig;
import update.UpdateAppUtils;

public class MainActivity extends AppCompatActivity{

    private static final String TAG = "MainActivity";

    private ActivityMainBinding binding;
    private DownLoaderUtils util;
    private String apkUrl = "http://gdown.baidu.com/data/wisegame/089002f01757d5e4/baidu_49284352.apk";
    private String updateTitle = "发现新版本V2.0.0";
    private String updateContent = "1、Kotlin重构版\n2、支持自定义UI\n3、增加md5校验\n4、更多功能等你探索";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DownLoaderManager.init(this);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.updateBtn.setOnClickListener(v -> updateApp());
        binding.downloadApk.setOnClickListener(v -> downloadApk());
        binding.downloadFile.setOnClickListener(v -> downloadFile());
        util = new DownLoaderUtils(this);
    }

    private void updateApp() {
        UpdateConfig updateConfig = new UpdateConfig();
        updateConfig.setCheckWifi(true);
        updateConfig.setNeedCheckMd5(true);
        updateConfig.setNotifyImgRes(R.mipmap.download);

        UiConfig uiConfig = new UiConfig();
        uiConfig.setUiType(UiType.PLENTIFUL);
        UpdateAppUtils
                .getInstance()
                .apkUrl(apkUrl)
                .updateTitle(updateTitle)
                .updateContent(updateContent)
                .uiConfig(uiConfig)
                .updateConfig(updateConfig)
                .setMd5CheckResultListener(new Md5CheckResultListener() {
                    @Override
                    public void onResult(boolean b) {

                    }
                })
                .setUpdateDownloadListener(new UpdateDownloadListener() {
                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onDownload(int i) {
                        Log.d(TAG, "进度：" + i);
                    }

                    @Override
                    public void onFinish() {

                    }

                    @Override
                    public void onError(Throwable throwable) {

                    }
                }).update();
    }

    private void downloadApk() {
        util.downloadApk("百度","http://gdown.baidu.com/data/wisegame/089002f01757d5e4/baidu_49284352.apk",true);
    }

    private void downloadFile() {
        util.downloadFile("应用宝","http://gdown.baidu.com/data/wisegame/0995fa2e64afa17e/yingyongbao_7382130.apk","ddd");
    }
}